package cn.edu.scujcc.customofsichuan.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.scujcc.customofsichuan.Entity.Collection;
import cn.edu.scujcc.customofsichuan.Listener.ItemClickListener;
import cn.edu.scujcc.customofsichuan.R;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.myCollectionHolder> {

    private ItemClickListener listener;
    List<Collection> collectionList;
    Context context;

    public CollectionAdapter(List<Collection> collectionList) {
        this.collectionList = collectionList;
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public myCollectionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_item, parent, false);
        context = view.getContext();
        myCollectionHolder holder=new myCollectionHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull myCollectionHolder holder, @SuppressLint("RecyclerView") int position) {
        Collection collection=collectionList.get(position);
        holder.collection_image.setImageResource(collection.getCollection_image());
        holder.collection_name.setText(collection.getName());
        holder.collection_time.setText(String.valueOf(collection.getCollection_time()));

        //给每个子项目添加监听事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onItemClick(view,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return collectionList.size();
    }

    class myCollectionHolder extends RecyclerView.ViewHolder{

        ImageView collection_image;
        TextView collection_name;
        TextView collection_time;
        ImageView collection_more;

        public myCollectionHolder(@NonNull View itemView) {
            super(itemView);
            collection_image = itemView.findViewById(R.id.collection_image);
            collection_name = itemView.findViewById(R.id.collection_name);
            collection_time = itemView.findViewById(R.id.collection_time);
            collection_more = itemView.findViewById(R.id.collection_more);
        }
    }

}
